package com.htthorizon.system.service.impl;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.date.DateTime;
import com.htthorizon.common.core.constant.CacheConstants;
import com.htthorizon.common.core.exception.LogCapcityOverException;
import com.htthorizon.common.redis.service.RedisService;
import com.htthorizon.system.api.domain.SysLogininfor;
import com.htthorizon.system.api.exception.LogOverException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.htthorizon.system.api.domain.SysOperLog;
import com.htthorizon.system.mapper.SysOperLogMapper;
import com.htthorizon.system.service.ISysOperLogService;

/**
 * 操作日志 服务层处理
 * 
 * @author mark
 */
@Service
@Slf4j
public class SysOperLogServiceImpl implements ISysOperLogService
{
    @Autowired
    private SysOperLogMapper operLogMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 新增操作日志
     * 
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public int insertOperlog(SysOperLog operLog)
    {
        Integer logNum = redisService.getCacheObject(CacheConstants.LOG_NUM);
        try {
            double capcity = this.capcity();
            if (!Objects.isNull(logNum)&&logNum < capcity){
                operLog.setLevel(1);
                operLog.setType("系统级(日志超出容量)");
            }
        }catch (Exception e){
            System.out.println("日志错误"+e);
            throw new LogOverException();
        }finally {
            return operLogMapper.insertOperlog(operLog);
        }


    }

    /**
     * 查询系统操作日志集合
     * 
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog)
    {
        return operLogMapper.selectOperLogList(operLog);
    }

    /**
     * 批量删除系统操作日志
     * 
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     * 
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog();
    }

    @Override
    public int count() {
        return operLogMapper.count();
    }

    @Override
    public void insertOperLogs(List<SysOperLog> sysOperLogs) {
        operLogMapper.insertOperLogs(sysOperLogs);
    }

    @Override
    public List<SysOperLog> selectByDate(DateTime offset) {
        return operLogMapper.selectByDate(offset);
    }

    private double capcity() {
        return operLogMapper.capcity();
    }

    @Override
    @Async
    public void insertBacth(List<SysOperLog> list) {
        operLogMapper.insertBacth(list);
    }

    @Override
    public List<SysOperLog> selectByDate2(DateTime offset) {
        return  operLogMapper.selectByDate2(offset);
    }

    @Override
    @Async
    public void insertBacth2(List<SysOperLog> list) {
        for (SysOperLog sysOperLog : list) {
            try{
                operLogMapper.insertOperlog2(sysOperLog);

            }catch (Exception e){
                log.info("跳过测试数据");
            }

        }
    }

    @Override
    @Async
    public void insertBacth3(List<SysOperLog> list) {
        for (SysOperLog sysOperLog : list) {
            try{
                operLogMapper.insertOperlog3(sysOperLog);

            }catch (Exception e){
                log.info("跳过测试数据");
            }

        }
    }

    @Override
    public boolean getLogCapcity() {
        double capcity = this.capcity();
        Integer cacheObject = redisService.getCacheObject("log_num");
        Double v = Double.valueOf(cacheObject);
        if (capcity<v){
            return true;
        }
        return false;
    }

    @Override
    public void truncate() {
        operLogMapper.truncate();
    }
}
